package com.mdx.page;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

/**
 * 页面工具
 * @author Administrator
 *
 */
public class PageUtils {
	
	public static final String USER_STR = "user";
	public static final String MASTER_STR = "master";
	public static final String ADMIN_STR = "admin";
	public static final String FILE_PATH = "E:/码大侠科技资料/项目资料/球迷财富圈/toPage.java";

	/**
	 * 通过文件名称，文件内容生成文件
	 * @param fileName
	 * @param classContent
	 */
	public static void generateFile (String fileName, String fileContent) {
		
		File file = new File(fileName);
		BufferedOutputStream outputStream = null;
		//如果文件不存在创建文件
		try {
			if(!file.exists()) {
				file.createNewFile();
			}
			outputStream = new BufferedOutputStream(new FileOutputStream(file));
			outputStream.write(fileContent.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			//关闭流文件
			try {
				if(outputStream != null) {
					outputStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 跳转页面方法
	 * @return
	 */
	public static String toPageFunc(String pageName, String pathName) {
		StringBuilder sb = new StringBuilder();
		pageName = pageName.substring(0, pageName.indexOf(".html"));
		sb.append("	/**\r\n" + 
				"	 * 跳转到" + pageName + "\r\n" + 
				"	 *@author huangy\r\n" + 
				"	 *@date 2018年12月20日\r\n" + 
				"	 *@return ModelAndView\r\n" + 
				"	 *@throws\r\n" + 
				"	 */\r\n" + 
				"	@RequestMapping(\"/" + pageName + "\")\r\n" + 
				"	public ModelAndView to" + pageName.replace("-", "") + "() {\r\n" + 
				"		return new ModelAndView(\"" + pathName + "/" + pageName + "\");\r\n" + 
				"	}\r\n");
		return sb.toString();
	}
	
	/**
	 * 批量生成页面方法
	 * @param pageNames
	 * @param pathName
	 */
	public static void generatePageFunc(String [] pageNames, String pathName) {
		StringBuilder sb = new StringBuilder();
		if(pageNames != null && pageNames.length > 0) {
			for (int i = 0; i < pageNames.length; i++) {
				sb.append(toPageFunc(pageNames[i], pathName));
			}
		}
		generateFile(FILE_PATH, sb.toString());
	}
	
	public static void main(String[] args) {
		//生成用户页面跳转方法
		//generatePageFunc(PageConstants.PAGE_NAME_USER, USER_STR);
		//生成师傅页面跳转方法
		//generatePageFunc(PageConstants.PAGE_NAME_MASTER, MASTER_STR);
		//生成管理员页面跳转方法
		generatePageFunc(PageConstants.PAGE_NAME_ADMIN, ADMIN_STR);
	}
	
}
